package com.taoym.voucher.job.controller;

import com.taoym.voucher.common.response.Response;
import com.taoym.voucher.job.domain.SysQuartzJob;
import com.taoym.voucher.job.entity.dto.AddJobDTO;
import com.taoym.voucher.job.entity.vo.GetQuartzJobVO;
import com.taoym.voucher.job.service.JobService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * @coder taoym
 * @datetime 2021/6/27 13:18
 * @desc 任务控制器
 */
@RestController
@RequestMapping("/job")
@Validated
public class JobController {
    @Autowired
    private JobService jobService;

    /**
     * @coder taoym
     * @datetime 2021/7/1 22:44
     * @desc 添加任务
     */
    @PostMapping("/add")
    public Response<Integer> addJob(@RequestBody @Valid AddJobDTO data) {
        return Response.success(jobService.addJob(data));
    }

    /**
     * @coder taoym
     * @datetime 2021/7/1 22:44
     * @desc 查询所有任务
     */
    @GetMapping("/list")
    public Response<List<GetQuartzJobVO>> getQuartzJobs() {
        return Response.success(jobService.quartzJobs());
    }

    /**
     * @coder taoym
     * @datetime 2021/7/1 22:44
     * @desc 开始任务
     */
    @GetMapping("/start")
    public Response<Integer> startJob(@RequestParam("id") @NotNull(message = "任务编号不能为空") Integer id) {
        return Response.success(jobService.startJob(id));
    }

    /**
     * @coder taoym
     * @datetime 2021/7/1 22:44
     * @desc 停止任务
     */
    @GetMapping("/stop")
    public Response<Integer> stopJob(@RequestParam("id") @NotNull(message = "任务编号不能为空") Integer id) {
        return Response.success(jobService.stopJob(id));
    }

    /**
     * @coder taoym
     * @datetime 2021/7/1 22:44
     * @desc 继续任务
     */
    @GetMapping("/proceed")
    public Response<Integer> proceed(@RequestParam("id") @NotNull(message = "任务编号不能为空") Integer id) {
        return Response.success(jobService.proceed(id));
    }

    /**
     * @coder taoym
     * @datetime 2021/7/1 22:44
     * @desc 删除任务
     */
    @GetMapping("/delete")
    public Response<Integer> delete(@RequestParam("id") @NotNull(message = "任务编号不能为空") Integer id) {
        return Response.success(jobService.delete(id));
    }

    /**
     * @coder taoym
     * @datetime 2021/7/17 21:37
     * @desc 重置任务状态
     */
    @GetMapping("/reset")
    public Response<Integer> resetJobStatus(@RequestParam("id") @NotNull(message = "编号不能为空") Integer id) {
        return Response.success(jobService.resetJobStatus(id));
    }
}
